"""
Excel 写入示例
"""

from faker import Faker

from my_typing import *
from utils.data_utils import ExcelHeaderInfo
from utils.excel_utils import ExcelWriter


def _get_fake_data() -> tuple[list[ExcelHeaderInfo], ListDict]:
    """ 获取假数据 """

    header_info_list: list[ExcelHeaderInfo] = [
        ExcelHeaderInfo("序号", "idx", int),
        ExcelHeaderInfo("姓名", "name", str),
        ExcelHeaderInfo("性别", "gender", str),
        ExcelHeaderInfo("手机号", "phone", str),
        ExcelHeaderInfo("邮箱", "email", str),
        ExcelHeaderInfo("地址", "address", str),
    ]
    """ 标题信息列表 """

    fake: Faker = Faker("zh_CN")
    """ Fake 假数据实例（区域为：中国） """

    data_list: ListDict = [
        {
            "idx": idx,
            "name": fake.name(),
            "gender": fake.random_element(["男", "女"]),
            "phone": fake.phone_number(),
            "email": fake.email(),
            "address": fake.address(),
        }
        for idx in range(1, 21)
    ]
    """ 生成的 20 条假数据 """

    return header_info_list, data_list


def write_user_info_to_excel_with_file_name():
    """ 将用户信息写入指定 Excel 文件中 """

    header_info_list, data_list = _get_fake_data()
    file_name: str = "./_excel_writer_demo_1.xlsx"

    handler: ExcelWriter = ExcelWriter(
        file_name=file_name,
        header_info_list=header_info_list,
        data_list=data_list,
    )
    """ ExcelWriter 实例 """

    # 写入数据
    handler.write()


def write_user_info_to_excel_with_bytes_io():
    """ 将用户信息写入 Excel 文件中，并保存为一个二进制文件 """

    header_info_list, data_list = _get_fake_data()

    file_io: BytesIO = BytesIO()
    """ 二进制文件 """

    handler: ExcelWriter = ExcelWriter(
        file_name=file_io,
        header_info_list=header_info_list,
        data_list=data_list,
    )
    """ ExcelWriter 实例 """

    # 写入数据
    handler.write()

    # 写入到本地文件
    with open("./_excel_writer_demo_2.xlsx", mode="wb") as f:
        f.write(file_io.getvalue())


if __name__ == "__main__":
    write_user_info_to_excel_with_file_name()
    write_user_info_to_excel_with_bytes_io()
